import * as Cesium from 'cesium/Cesium'

/**
 *  cesium tool
 * 实例模式 创建
 * @constructor
 */
let Utils = {viewer:{}};

/**
 * 单例模式 创建
 * @param viewer
 * @constructor
 */
// function Utils(viewer) {this.viewer = viewer}
/**
 * UUID生成
 * @returns {string}
 */
Utils.generateUUID = function () {
    return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (e) {
        var t = 16 * Math.random() | 0;
        return ("x" === e ? t : 3 & t | 8).toString(16)
    })
};

/**
 * 墨卡托投影转经纬度坐标
 * @param mercator
 */
Utils.convertWebMercatorToLonlat = function (mercator) {
    var cartographic = new Cesium.Cartographic(mercator[0],mercator[1],mercator[22])
    var lonlat =   [cartographic.longitude / Math.PI * 180,cartographic.latitude / Math.PI * 180];
    return lonlat;
}
/**
 * 屏幕坐标转世界坐标
 * @param windowPosition
 */
Utils.convertWindowToWorld = function (windowPosition) {
    var pick =  this.viewer.scene.globe.pick(this.viewer.camera.getPickRay(windowPosition), this.viewer.scene);
    return [pick.x,pick.y,pick.z];
}
/**
 * 经纬度转世界坐标
 * @param lonLat
 * @param h
 */
Utils.convertLonlatToWorld = function (lonLat, h) {
    var ellipsoid=this.viewer.scene.globe.ellipsoid;
    var cartographic=Cesium.Cartographic.fromDegrees(lonLat[0],lonLat[1],h?h:0);
    var cartesian3=ellipsoid.cartographicToCartesian(cartographic);
    return [cartesian3.x,cartesian3.y,cartesian3.z];
}
/**
 * 世界坐标转经纬度
 * @param pos
 */
Utils.convertWorldToLonlat = function (pos) {
    var ellipsoid = this.viewer.scene.globe.ellipsoid;
    var cartesian3 = new Cesium.Cartesian3(pos[0], pos[1], pos[2]);
    var cartographic = ellipsoid.cartesianToCartographic(cartesian3);
    var lat = Cesium.Math.toDegrees(cartographic.latitude);
    var lng = Cesium.Math.toDegrees(cartographic.longitude);
    var alt = cartographic.height;
    return [lng,lat, alt];
}

export default Utils
